############################################################
## Fraga, Juenke, and Shah 2019: One Run Leads to Another?
## Replication Code: January 2, 2019
## Functions used to generate results
############################################################

# Function for extracting point of equality and SE based on loess
getEquality <- function(model, probability){
	modelout <- as.data.frame(cbind(seq(0,1,0.0001),predict(model, newdata=seq(0,1,0.0001), se=TRUE)$fit, predict(model, newdata=seq(0,1,0.0001), se=TRUE)$se))
	probs <- subset(modelout, V2 == max(modelout$V2[!is.na(modelout$V2) & modelout$V2 <= probability]) | V2 == min(modelout$V2[!is.na(modelout$V2) & modelout$V2 >= probability]))
	return(as.data.frame(cbind(approx(probs$V2, probs$V1, xout=probability)$y, approx(probs$V2, probs$V3, xout=probability)$y)))
	}
	
# Functions allowing generation of point of equality and SE with fixed thresholds
getThresholdsW <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$white_incprop >= i)
		dataN <- subset(data, data$white_incprop < i)
		
		model <- loess(white_cand ~ white_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(white_cand ~ white_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderW")){
			bigholderW <<- rbind(bigholderW, holder)
		} else {bigholderW <<- holder}
	}
	return(bigholderW)
	rm(bigholderW)
}
getThresholdsB <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$black_incprop >= i)
		dataN <- subset(data, data$black_incprop < i)
		
		model <- loess(black_cand ~ black_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(black_cand ~ black_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderB")){
			bigholderB <<- rbind(bigholderB, holder)
		} else {bigholderB <<- holder}
	}
	return(bigholderB)
	rm(bigholderB)
}
getThresholdsL <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$latino_incprop >= i)
		dataN <- subset(data, data$latino_incprop < i)
		
		model <- loess(latino_cand ~ latino_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(latino_cand ~ latino_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderL")){
			bigholderL <<- rbind(bigholderL, holder)
		} else {bigholderL <<- holder}
	}
	return(bigholderL)
	rm(bigholderL)
}
getThresholdsA <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$asian_incprop >= i)
		dataN <- subset(data, data$asian_incprop < i)
		
		model <- loess(asian_cand ~ asian_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(asian_cand ~ asian_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderA")){
			bigholderA <<- rbind(bigholderA, holder)
		} else {bigholderA <<- holder}
	}
	return(bigholderA)
	rm(bigholderA)
}

# Functions allowing generation of point of equality and SE with fixed thresholds
## Democratic Candidates Only
getThresholdsWD <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$white_incprop_D >= i)
		dataN <- subset(data, data$white_incprop_D < i)
		
		model <- loess(white_cand ~ white_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(white_cand ~ white_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderW")){
			bigholderW <<- rbind(bigholderW, holder)
		} else {bigholderW <<- holder}
	}
	return(bigholderW)
	rm(bigholderW)
}
getThresholdsBD <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$black_incprop_D >= i)
		dataN <- subset(data, data$black_incprop_D < i)
		
		model <- loess(black_cand ~ black_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(black_cand ~ black_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderB")){
			bigholderB <<- rbind(bigholderB, holder)
		} else {bigholderB <<- holder}
	}
	return(bigholderB)
	rm(bigholderB)
}
getThresholdsLD <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$latino_incprop_D >= i)
		dataN <- subset(data, data$latino_incprop_D < i)
		
		model <- loess(latino_cand ~ latino_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(latino_cand ~ latino_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderL")){
			bigholderL <<- rbind(bigholderL, holder)
		} else {bigholderL <<- holder}
	}
	return(bigholderL)
	rm(bigholderL)
}
getThresholdsAD <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$asian_incprop_D >= i)
		dataN <- subset(data, data$asian_incprop_D < i)
		
		model <- loess(asian_cand ~ asian_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(asian_cand ~ asian_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderA")){
			bigholderA <<- rbind(bigholderA, holder)
		} else {bigholderA <<- holder}
	}
	return(bigholderA)
	rm(bigholderA)
}

# Functions allowing generation of point of equality and SE with fixed thresholds
## Republican Candidates Only
getThresholdsWR <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$white_incprop_R >= i)
		dataN <- subset(data, data$white_incprop_R < i)
		
		model <- loess(white_cand ~ white_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(white_cand ~ white_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderW")){
			bigholderW <<- rbind(bigholderW, holder)
		} else {bigholderW <<- holder}
	}
	return(bigholderW)
	rm(bigholderW)
}
getThresholdsBR <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$black_incprop_R >= i)
		dataN <- subset(data, data$black_incprop_R < i)
		
		model <- loess(black_cand ~ black_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(black_cand ~ black_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderB")){
			bigholderB <<- rbind(bigholderB, holder)
		} else {bigholderB <<- holder}
	}
	return(bigholderB)
	rm(bigholderB)
}
getThresholdsLR <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$latino_incprop_R >= i)
		dataN <- subset(data, data$latino_incprop_R < i)
		
		model <- loess(latino_cand ~ latino_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(latino_cand ~ latino_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderL")){
			bigholderL <<- rbind(bigholderL, holder)
		} else {bigholderL <<- holder}
	}
	return(bigholderL)
	rm(bigholderL)
}
getThresholdsAR <- function(rangelo, rangehi, dataset){
	data <- dataset
	for(i in seq(rangelo, rangehi, 0.01)){
		dataY <- subset(data, data$asian_incprop_R >= i)
		dataN <- subset(data, data$asian_incprop_R < i)
		
		model <- loess(asian_cand ~ asian_pct, data=dataN)
		noInc <- getEquality(model, 0.5)
		model <- loess(asian_cand ~ asian_pct, data=dataY)
		Inc <- getEquality(model, 0.5)
		
		holder <- as.data.frame(cbind(noInc, Inc, i))
		names(holder) <- c("noInc", "noInc_SE", "Inc", "Inc_SE", "Threshold")
		if(exists("bigholderA")){
			bigholderA <<- rbind(bigholderA, holder)
		} else {bigholderA <<- holder}
	}
	return(bigholderA)
	rm(bigholderA)
}